package service.associate
 {
     import mx.controls.Alert;
     import mx.rpc.events.FaultEvent;
     import mx.rpc.events.ResultEvent;
     import mx.rpc.remoting.mxml.RemoteObject;
 
     import common.Config;
     import model.associate.AssociateDataTable;
     import model.associate.AssociateDataType;
     import model.associate.AssociateResult;
     import model.associate.AssociateDataSet;
 
       public class AssociateService
     {
         [Bindable]
         public var associateDataTable:AssociateDataTable;
         [Bindable]
         public var associateDataType:AssociateDataType;
         [Bindable]
         public var associateResult:AssociateResult;
         [Bindable]
         public var associateDataSet:AssociateDataSet; 

         private var remoteObject:RemoteObject;
         private static var _instance:AssociateService;
 
         public function AssociateService(privateClass:PrivateClass)
         {
             this.remoteObject = new RemoteObject();
             this.remoteObject.destination = "AssociateGateway"
             this.remoteObject.endpoint = Config.serverUrl;
             this.remoteObject.requestTimeout = Config.requestTimeout;
             this.remoteObject.concurrency = Config.concurrency;
             this.remoteObject.showBusyCursor = true;
 
             this.associateDataTable = new AssociateDataTable();
             this.associateDataType = new AssociateDataType();
             this.associateResult = new AssociateResult();
             this.associateDataSet = new AssociateDataSet();
         }
 
         public static function getInstance():AssociateService
         {
             if(AssociateService._instance == null)
             {
                 AssociateService._instance = new AssociateService(
                                                 new PrivateClass());
             }
             return AssociateService._instance;
         }
 
         public function addListeners(resultFunc:Function,
                                      faultFunc:Function):void
         {
             this.remoteObject.addEventListener(ResultEvent.RESULT,
                                                resultFunc);
             this.remoteObject.addEventListener(FaultEvent.FAULT,
                                                faultFunc);
         }
 
         public function removeListeners(resultFunc:Function,
                                         faultFunc:Function):void
         {
             this.remoteObject.removeEventListener(ResultEvent.RESULT,
                                                   resultFunc);
             this.remoteObject.removeEventListener(FaultEvent.FAULT,
                                                   faultFunc);
         }
 
         //--------------------------------------------------------------------
         // TODO: get the data table
         public function getDataTable():void
         {
             this.addListeners(getDataTableResult, getDataTableFault);
             this.remoteObject.getDataTable();
         }
 
         public function getDataTableResult(event:ResultEvent):void
         {
             this.removeListeners(getDataTableResult, getDataTableFault);
             this.associateDataTable.update(event.message.body.dataTable);
         }
 
         public function getDataTableFault(event:FaultEvent):void
         {
             this.removeListeners(getDataTableResult, getDataTableFault);
             Alert.show("Get data table fault");
         }
         //--------------------------------------------------------------------
         // TODO: get the data type
         public function getDataType(id:int):void
         {
             this.addListeners(getDataTypeResult, getDataTypeFault);
             this.remoteObject.getDataType(id);
         }
 
         public function getDataTypeResult(event:ResultEvent):void
         {
             this.removeListeners(getDataTypeResult, getDataTypeFault);
             this.associateDataType.update(event.message.body.dataType);
         }
 
         public function getDataTypeFault(event:FaultEvent):void
         {
             this.removeListeners(getDataTypeResult, getDataTypeFault);
             Alert.show("Get data type fault");
         }

         // TODO: get the dataset
         public function getDataSet(id:int):void
         {
             this.addListeners(getDataSetResult, getDataSetFault);
             this.remoteObject.getDataSet(id);
         }

         public function getDataSetResult(event:ResultEvent):void
         {
             this.removeListeners(getDataSetResult, getDataSetFault);
             this.associateDataSet.update(event.message.body.dataSet);
         }
 
         public function getDataSetFault(event:FaultEvent):void
         {
             this.removeListeners(getDataSetResult, getDataSetFault);
             Alert.show("Get data set fault");
         }


         //--------------------------------------------------------------------
         //TODO:Apriori
         public function apriori(dataId:int, min_sup:int, min_conf:int):void
         {
             this.addListeners(aprioriResult, aprioriFault);
             this.remoteObject.apriori(dataId, min_sup, min_conf);    
         }
 
         public function aprioriResult(event:ResultEvent):void
         {
             this.removeListeners(aprioriResult, aprioriFault);
             this.associateResult.update(event.message.body.result);
         }
 
         public function aprioriFault(event:FaultEvent):void
         {
             this.removeListeners(aprioriResult, aprioriFault);
             Alert.show("Apriori fault");
         }
     }
 }
 
 class PrivateClass
 {
     public function PrivateClass() {}
 }
 
